﻿using DevExpress.Data;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DXApplicationTangche.UC.面料审核.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.面料审核.生产计划UC
{
    public partial class XtraUserControl生产计划视图 : DevExpress.XtraEditors.XtraUserControl
    {
        private List<MrpInventoryDetailDTO> mrpInventoryDetailDTOs = new List<MrpInventoryDetailDTO>();
        private List<MrpResultDTO> mrpResultDTOs = new List<MrpResultDTO>();

        public XtraUserControl生产计划视图()
        {
            InitializeComponent();
        }
        public XtraUserControl生产计划视图(MrpInventoryCalculateResultDTO mrpInventoryCalculateResultDTO)
        {
            InitializeComponent();
            this.在制品显示(mrpInventoryCalculateResultDTO);
            this.生产计划视图显示(mrpInventoryCalculateResultDTO);
            this.出入库显示(mrpInventoryCalculateResultDTO);
        }
        private void 在制品显示(MrpInventoryCalculateResultDTO mrpInventoryCalculateResultDTO)
        {
            this.mrpResultDTOs = mrpInventoryCalculateResultDTO.mrpResultDTOS;
            foreach (MrpResultDTO mrpResultDTO in mrpInventoryCalculateResultDTO.mrpResultDTOS)
            {
                try { mrpResultDTO.styleSizeCd = mrpResultDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
            }
            this.pivotGridControl1.DataSource = mrpInventoryCalculateResultDTO.mrpResultDTOS;
        }
        private void 生产计划视图显示(MrpInventoryCalculateResultDTO mrpInventoryCalculateResultDTO)
        {
            this.mrpInventoryDetailDTOs = mrpInventoryCalculateResultDTO.mrpInventoryDetailDTOS;
            foreach (MrpInventoryDetailDTO mrpInventoryDetailDTO in this.mrpInventoryDetailDTOs)
            {
                try { mrpInventoryDetailDTO.styleSizeCd = mrpInventoryDetailDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
            }
            this.gridControl产成品明细.DataSource = this.mrpInventoryDetailDTOs;
            this.bandedGridView1.ExpandAllGroups();
            this.bandedGridView1.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Count, "barcode", null, "(数量总计= {0})"), });
            this.bandedGridView1.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Sum, "price", null, "(金额总计= {0})"), });
        }
        private void 出入库显示(MrpInventoryCalculateResultDTO mrpInventoryCalculateResultDTO)
        {
            //成衣库存
            List<GarmentStockStatusDTO> garmentStockStatusDTOs = mrpInventoryCalculateResultDTO.garmentStockStatusDTOs;
            //计算抖音差异数量
            foreach (GarmentStockStatusDTO garmentStockStatusDTO in garmentStockStatusDTOs)
            {
                garmentStockStatusDTO.douyinRequestQuantity = garmentStockStatusDTO.garmentOrderQuatity - garmentStockStatusDTO.douyinOrderQuantity;
                garmentStockStatusDTO.garmentRequestQuantity = garmentStockStatusDTO.wipQuantity + garmentStockStatusDTO.stockQuantity - garmentStockStatusDTO.douyinprepareQuantity;
            }
            this.gridControl库存明细.DataSource = garmentStockStatusDTOs;
        }
        private void bandedGridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                else
                {
                    e.DisplayText = "样衣无尺寸定义";
                }
            }
            if (e.Column.Name.Equals("bandedGridColumn工序名称"))
            {
                if (String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    e.DisplayText = "未投产";
                }
            }
            if (e.Column.Name.Equals("gridColumn首次时间"))
            {
                if ("0001/1/1".Equals(e.DisplayText))
                {
                    e.DisplayText = "";
                }
            }
            if (e.Column.Name.Equals("gridColumn最新时间"))
            {
                if ("0001/1/1".Equals(e.DisplayText))
                {
                    e.DisplayText = "";
                }
            }
        }

        private void 查看成衣库存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String styleNameCn = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "styleNameCn").ToString();
            String materialCode = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "materialCode").ToString();
            String barcode = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "barcode").ToString();
            List<GarmentStockDTO> garmentStockDTOs = 面料审核Service.getGarmentStocckByBarcode(barcode);
            new Form成衣同款库存(barcode, styleNameCn, materialCode, garmentStockDTOs).ShowDialog();
        }

        private void 查看面料库存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            List<String> materials = new List<string>();
            String materialCode = this.bandedGridView1.GetRowCellValue(this.bandedGridView1.FocusedRowHandle, "materialCode").ToString();
            materials.Add(materialCode);
            new Form成衣面料库存(materials).ShowDialog();
        }

        private void 导出生产计划视图ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.mrpInventoryDetailDTOs.Count() == 0)
            {
                MessageBox.Show("请先选择导出数据");
                return;
            }
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出生产计划视图";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                this.gridControl产成品明细.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        /// <summary>
        /// 同款、同面料、同尺码的所有工序信息
        /// </summary>
        /// <param name="styleCode">款号</param>
        /// <param name="materialCode">面料号</param>
        /// <param name="styleSizeCd">尺码</param>
        private void 工序显示刷新(String styleCode, String materialCode, String styleSizeCd)
        {
            List<MrpResultDTO> mrpResultDTOs显示 = new List<MrpResultDTO>();
            foreach (MrpResultDTO mrpResultDTO in this.mrpResultDTOs)
            {
                try { mrpResultDTO.styleSizeCd = mrpResultDTO.styleSizeCd.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                if (mrpResultDTO.styleNameCn.Contains(styleCode)
                    && mrpResultDTO.materialCode.Equals(materialCode)
                    && styleSizeCd.Equals(mrpResultDTO.styleSizeCd))
                {
                    mrpResultDTOs显示.Add(mrpResultDTO);
                }
            }
            this.pivotGridControl1.DataSource = mrpResultDTOs显示;
        }
        /// <summary>
        /// 同款、同面料、同尺码的所有成衣信息
        /// </summary>
        /// <param name="styleCode">款号</param>
        /// <param name="materialCode">面料号</param>
        /// <param name="styleSizeCd">尺码</param>
        private void 明细显示刷新(String styleCode, String materialCode, String styleSizeCd)
        {
            List<MrpInventoryDetailDTO> mrpInventoryDetailDTOs显示 = new List<MrpInventoryDetailDTO>();
            foreach (MrpInventoryDetailDTO mrpInventoryDetailDTO in this.mrpInventoryDetailDTOs)
            {
                if (mrpInventoryDetailDTO.styleNameCn.Contains(styleCode)
                    && mrpInventoryDetailDTO.materialCode.Equals(materialCode)
                    && styleSizeCd.Equals(mrpInventoryDetailDTO.styleSizeCd))
                {
                    mrpInventoryDetailDTOs显示.Add(mrpInventoryDetailDTO);
                }
            }
            this.gridControl出入库.DataSource = mrpInventoryDetailDTOs显示;
            this.bandedGridView2.ExpandAllGroups();
            //表头分组添加总计
            this.bandedGridView2.GroupSummary.Clear();
            this.bandedGridView2.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Count, "barcode", null, "(件数= {0}  )"), });
            this.bandedGridView2.RefreshData();
        }
        /// <summary>
        /// 同款、同面料的所有尺码成衣信息
        /// </summary>
        /// <param name="styleCode">款号</param>
        /// <param name="materialCode">面料号</param>
        private void 产成品显示刷新(String styleCode, String materialCode)
        {
            List<MrpInventoryDetailDTO> mrpInventoryDetailDTOs显示 = new List<MrpInventoryDetailDTO>();
            foreach (MrpInventoryDetailDTO mrpInventoryDetailDTO in this.mrpInventoryDetailDTOs)
            {
                if (mrpInventoryDetailDTO.styleNameCn.Contains(styleCode)
                    && mrpInventoryDetailDTO.materialCode.Equals(materialCode)
                    )
                {
                    mrpInventoryDetailDTOs显示.Add(mrpInventoryDetailDTO);
                }
            }
            this.gridControl产成品明细.DataSource = mrpInventoryDetailDTOs显示;
            this.gridColumn尺码.GroupIndex = 0;
            this.bandedGridView1.ExpandAllGroups();
            //表头分组添加总计
            this.bandedGridView1.GroupSummary.Clear();
            this.bandedGridView1.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Count, "barcode", null, "(数量总计= {0})"), });
            this.bandedGridView1.GroupSummary.AddRange(new GridSummaryItem[] { new GridGroupSummaryItem(SummaryItemType.Sum, "price", null, "(金额总计= {0})"), });
        }

        private void bandedGridView库存明细_DoubleClick(object sender, EventArgs e)
        {
            String styleCode = this.bandedGridView库存明细.GetRowCellValue(this.bandedGridView库存明细.FocusedRowHandle, "styleCode").ToString();
            String materialCode = this.bandedGridView库存明细.GetRowCellValue(this.bandedGridView库存明细.FocusedRowHandle, "materialCode").ToString();
            String styleSizeCd = this.bandedGridView库存明细.GetRowCellValue(this.bandedGridView库存明细.FocusedRowHandle, "styleSizeCd").ToString().Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", "");
            this.明细显示刷新(styleCode, materialCode, styleSizeCd);
            this.工序显示刷新(styleCode, materialCode, styleSizeCd);
            this.产成品显示刷新(styleCode, materialCode);
        }

        private void bandedGridView库存明细_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Name.Equals("gridColumn尺码1"))
            {
                if (!String.IsNullOrWhiteSpace(e.DisplayText))
                {
                    try { e.DisplayText = e.DisplayText.Replace("IGS_GROUP_SIZE-", "").Replace("EGS_GROUP_SIZE-", ""); } catch { }
                }
                else
                {
                    e.DisplayText = "样衣无尺寸定义";
                }
            }
        }

        private void bandedGridView库存明细_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            try
            {
                if (e.RowHandle >= 0)
                {
                    GarmentStockStatusDTO garmentStockStatusDTO = (GarmentStockStatusDTO)bandedGridView库存明细.GetRow(e.RowHandle);
                    int douyinRequest = garmentStockStatusDTO.douyinprepareQuantity + garmentStockStatusDTO.douyindeliveryQuantity + garmentStockStatusDTO.douyincompleteQuantity;
                    int stockQuantity = garmentStockStatusDTO.stockQuantity + garmentStockStatusDTO.wipQuantity;
                    if (garmentStockStatusDTO.garmentOrderQuatity < douyinRequest)
                    {
                        if (e.Column.Name.Equals("bandedGridColumn下单数量差值1"))
                        {
                            e.Appearance.BackColor = Color.DeepPink;
                            e.Appearance.ForeColor = Color.White;
                        }
                    }
                    if (garmentStockStatusDTO.douyinprepareQuantity > stockQuantity)
                    {
                        if (e.Column.Name.Equals("bandedGridColumn在制品数量差值1"))
                        {
                            e.Appearance.BackColor = Color.Orange;
                            e.Appearance.ForeColor = Color.White;
                        }
                    }
                }
            }
            catch { }
        }

        private void 导出成衣抬头数量信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出订单明细";
            fileDialog.Filter = "Excel文件(*.xlsx)|*.xlsx";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                //保存销售收款单、退款退货单、退款未退货单
                this.bandedGridView库存明细.ExportToXlsx(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("文件已保存至" + fileDialog.FileName, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
